from utils.font_size import FontSize
from components.text import *
from components.rect import *
from settings import *


class ExpressWayBaseComponent:

    def __init__(self, head_name, main_name, sub_name, head_bg_color, head_text_color) -> None:
        self.head_name = head_name
        self.main_name = main_name
        self.sub_name = sub_name
        self.head_bg_color = head_bg_color
        self.head_text_color = head_text_color
        self.main_text_height = 50 * SCALE
        self.main_text_width = FontSize.get_font_width(self.main_name, FONT_PATH_B, self.main_text_height)
        self.sub_text_height = int(self.main_text_height * 0.6)
        self.sub_text_width = FontSize.get_font_width(self.sub_name, FONT_PATH_B, self.sub_text_height)
        self.inset = 5 * SCALE
        self.stroke_width = 2 * SCALE
        self.r = 8 * SCALE
        self.head_height = 12 * SCALE
        self.head_text_size = 10 * SCALE
        self.head_text = self.head_name
        self.rect_width = max(self.main_text_width + self.sub_text_width + 2 * self.inset, self.height())
        self.head_text_space = max((self.rect_width - 2 * self.inset - len(self.head_text) * self.head_text_size) / (len(self.head_text) + 1), 0)
        head_ele = SpaceTextAComponent(self.head_text, self.head_text_color, self.head_text_size, self.head_text_space)
        self.rect_width = max(self.rect_width, head_ele.width() + 2 * self.inset)

    def width(self):
        return self.rect_width
    
    def height(self):
        return self.main_text_height + self.head_height
    
    def draw(self, container, x, y):
        name_start_x = x + (self.rect_width - (self.main_text_width + self.sub_text_width)) / 2
        main_rect = SingleStrokeRectComponent(self.rect_width, self.main_text_height + self.head_height, COLOR_GREEN, COLOR_WHITE, self.stroke_width, self.r)
        main_rect.draw(container, x, y)
        container.add(
            container.rect(
                insert=(x + self.stroke_width, y + self.stroke_width), 
                size=(self.rect_width - 2 * self.stroke_width, self.head_height), 
                rx=self.r - self.stroke_width, 
                ry=self.r - self.stroke_width, 
                fill=self.head_bg_color
            )
        )
        container.add(
            container.rect(
                insert=(x + self.stroke_width, y + self.head_height - self.r + 2 * self.stroke_width), 
                size=(self.rect_width - 2 * self.stroke_width, self.r - self.stroke_width), 
                rx=0, 
                ry=0, 
                fill=self.head_bg_color
            )
        )
        head = SpaceTextAComponent(self.head_text, self.head_text_color, self.head_text_size, self.head_text_space)
        head.draw(container, x + (self.rect_width - head.width()) / 2, y + self.stroke_width + (self.head_height - self.head_text_size) / 2)

        big_text = TextBComponent(self.main_name, COLOR_WHITE, self.main_text_height)
        big_text.draw(container, name_start_x, y + self.head_height)

        if len(self.sub_name):
            small_text = TextBComponent(self.sub_name, COLOR_WHITE, self.sub_text_height)
            small_text.draw(container, name_start_x + self.main_text_width, y + self.head_height + self.main_text_height * 0.32)


class NationalExpressWayComponent(ExpressWayBaseComponent):

    def __init__(self, main_name, sub_name) -> None:
        super().__init__('国家高速', main_name, sub_name, COLOR_RED, COLOR_WHITE)


class ProvincialExpressWayComponent(ExpressWayBaseComponent):

    def __init__(self, prov, main_name, sub_name) -> None:
        super().__init__('{}高速'.format(prov), main_name, sub_name, COLOR_YELLOW, COLOR_BLACK)